﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("请输入用户名");
                string username = Console.ReadLine();
                Console.WriteLine("请输入年龄");

                string age = Console.ReadLine();

                user user = new user()
                {
                    Username = username,
                    Age = int.Parse(age)
                };

                List<string> errorList = Validator.Validate(user);
                if (errorList.Count == 0)
                {
                    Console.WriteLine("验证通过");
                }
                else
                {
                    Console.WriteLine("验证失败");
                    foreach (string error in errorList)
                    {
                        Console.WriteLine($"- {error}");
                    }
                }
            }
        }

        //主体用户类

        class user
        {
            [Required("用户不能为空")]
            public string Username {  get; set; }
            [Range(10, 60)]
            public int Age {  get; set; }
        }

        [AttributeUsage(AttributeTargets.Property)]

        class RequiredAttribute : Attribute
        {
            public string ErrorMessage {  get; set; }
            public RequiredAttribute(string errorMessage = "属性不能为空") {
            ErrorMessage = errorMessage;
            }
        }
        [AttributeUsage(AttributeTargets.Property)]

        class RangeAttribute : Attribute
        {
            public int Min { get; set; }
            public int Max { get; set; }
            public string ErrorMsg { get; set; }
            public RangeAttribute(int min, int max, string errorMsg = "数据不能超出范围")
            {
                Min = min; Max = max; ErrorMsg = errorMsg;
            }
        }

        class Validator
        {
            public static List<string> Validate(object value)
            {
                List<string> errorList = new List<string>();
                //1. 先通过反射获得类型信息
                Type type = value.GetType();
                Type type1 = typeof(user);
                Type type2 = Type.GetType("反射和特性的使用3.user");
                user user = new user();
                user.Username = " ";

                foreach (PropertyInfo item in type.GetProperties())
                {
                    object infoValue = item.GetValue(value, null);

                    foreach(Attribute attribute in item.GetCustomAttributes())
                    {
                        if (attribute is RequiredAttribute required)
                        {
                            if (infoValue == null || string.IsNullOrEmpty(infoValue.ToString())) {
                                errorList.Add(required.ErrorMessage);
                            }
                        }

                        if (attribute is RangeAttribute rangeAttribute)
                        {
                            int intvalue = (int)infoValue;
                            if (intvalue > 60 || intvalue < 10)
                            {
                                errorList.Add(rangeAttribute.ErrorMsg);
                            }
                        }
                    }
                }
                return errorList;
            }
        }
    }
}
